package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;

import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ElderlyClassDTO;
import com.api.shopapi.service.ElderlyClassService;
import com.api.shopapi.service.GovernmentService;
import com.api.shopapi.service.StaffService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * 老人分类
 */
@RestController
@RequestMapping("ElderlyClass")
public class ElderlyClassController extends BaseController{

    @Autowired
    @Qualifier("ElderlyClassServiceImplOne")
    private ElderlyClassService elderlyClassService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("GovernmentServiceImplOne")
    private GovernmentService governmentService;



    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("name");
            int sort = jsonObject.getInteger("sort");
            double addBalance = jsonObject.getDouble("addBalance");
            int value =jsonObject.getIntValue("value");
            String staffCode = jsonObject.getString("staffCode");
            String governmentCode =jsonObject.getString("governmentCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ElderlyClass/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ElderlyClass elderlyClass1 =elderlyClassService.getByValue(value,governmentCode);
            if(elderlyClass1!=null){
                return failed("该政府下绑定值已被添加！");
            }

            ElderlyClass elderlyClass2=elderlyClassService.getByName(name,governmentCode);
            if(elderlyClass2!=null){
                return failed("该政府下类型已被添加！");
            }


            ElderlyClass elderlyClass =new ElderlyClass();
            elderlyClass.setElderlyClassCode(UUID.randomUUID().toString());
            elderlyClass.setName(name);
            elderlyClass.setValue(value);
            elderlyClass.setSort(sort);
            elderlyClass.setAddBalance(addBalance);
            elderlyClass.setStaffCode(staffCode);
            elderlyClass.setGovernmentCode(governmentCode);
            elderlyClassService.insert(elderlyClass);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);

        } catch (Exception e) {
            insertError("/ElderlyClass/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "GovAdd", method = RequestMethod.POST)
    public MyResult GovAdd(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name").trim();
            //int value =jsonObject.getIntValue("value");
            //double addBalance = jsonObject.getDouble("addBalance");
            int sort = jsonObject.getInteger("sort");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ElderlyClass/GovAdd");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

//            ElderlyClass elderlyClass1 =elderlyClassService.getByNameAndGovernmentCode(name,staff.getGovernmentCode());
//            if(elderlyClass1!=null){
//                return failed("该分类已被添加");
//            }
//
//            ElderlyClass elderlyClass2 =elderlyClassService.getByValueAndGovernmentCode(value,staff.getGovernmentCode());
//            if(elderlyClass2!=null){
//                return failed("该分类值已被添加");
//            }


            ElderlyClass elderlyClass =new ElderlyClass();
            elderlyClass.setElderlyClassCode(UUID.randomUUID().toString());
            elderlyClass.setName(name);
            //elderlyClass.setValue(value);
            elderlyClass.setSort(sort);
            //elderlyClass.setAddBalance(addBalance);
            elderlyClass.setGovernmentCode(staff.getGovernmentCode());
            elderlyClassService.insert(elderlyClass);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ElderlyClass/GovAdd",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }



    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String name = jsonObject.getString("name");
            int sort = jsonObject.getInteger("sort");
            double addBalance = jsonObject.getDouble("addBalance");
            String governmentCode =jsonObject.getString("governmentCode");
            String elderlyClassCode =jsonObject.getString("elderlyClassCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ElderlyClass/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ElderlyClass elderlyClass =elderlyClassService.getByElderlyClassCode(elderlyClassCode);

            ElderlyClass elderlyClass2=elderlyClassService.getByName(name,governmentCode);
            if(elderlyClass2!=null){
                if(!elderlyClass2.getElderlyClassCode().equals(elderlyClassCode)){
                    return failed("该政府下类型已被添加！");
                }
            }

            elderlyClass.setName(name);
            elderlyClass.setSort(sort);
            elderlyClass.setAddBalance(addBalance);
            elderlyClassService.update(elderlyClass);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ElderlyClass/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "GovEdit",method = RequestMethod.POST)
    public MyResult GovEdit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode =jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int sort = jsonObject.getInteger("sort");
            //double addBalance = jsonObject.getDouble("addBalance");

            String elderlyClassCode =jsonObject.getString("elderlyClassCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ElderlyClass/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

//            ElderlyClass elderlyClass2=elderlyClassService.getByNameAndGovernmentCode(name,staff.getGovernmentCode());
//            if(elderlyClass2!=null){
//                if(!elderlyClass2.getElderlyClassCode().equals(elderlyClassCode)){
//                    return failed("该分类已被添加");
//                }
//            }

            ElderlyClass elderlyClass =elderlyClassService.getByElderlyClassCode(elderlyClassCode);
            elderlyClass.setName(name);
            elderlyClass.setSort(sort);
            //elderlyClass.setAddBalance(addBalance);
            elderlyClassService.update(elderlyClass);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ElderlyClass/GovEdit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }



    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult getList(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ElderlyClass/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);
            String governmentCode = staff.getGovernmentCode();
            List<ElderlyClass> listElderlyClass = elderlyClassService.getList(governmentCode);

            return success(listElderlyClass);
        } catch (Exception e) {
            insertError("/ElderlyClass/List",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ElderlyClass/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PageInfo<ElderlyClassDTO> elderlyClassPageInfo =elderlyClassService.getListPage(pageIndex,pageSize);
            List<ElderlyClassDTO> elderlyClassList=elderlyClassPageInfo.getList();
            for (ElderlyClassDTO elderlyClassDTO:elderlyClassList){
                Government government =governmentService.getByGovernmentCode(elderlyClassDTO.getGovernmentCode());
                elderlyClassDTO.setGovernmentName(government.getName());
            }

            return success(elderlyClassPageInfo);
        } catch (Exception e) {
            insertError("/ElderlyClass/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "GetElderlyClass",method = RequestMethod.POST)
    public MyResult GetElderlyClass(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            int value = jsonObject.getIntValue("value");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ElderlyClass/GetElderlyClass");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Staff staff = staffService.getByStaffCode(staffCode);

            ElderlyClass elderlyClass = elderlyClassService.getByValue(value, staff.getGovernmentCode());

            return success(elderlyClass);

        } catch (Exception e) {
            insertError("/ElderlyClass/GetElderlyClass", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "AddElderlyClass",method = RequestMethod.GET)
    public String AddElderlyClass(){
            List<Government> governmentList = governmentService.getAllGovernmentCode();
            for (Government government : governmentList) {
                List<ElderlyClass> elderlyClassList = elderlyClassService.getList(government.getGovernmentCode());
                boolean flag=true;
                for (ElderlyClass elderlyClass : elderlyClassList) {
                    if (elderlyClass.getName().equals("默认")){
                        flag=false;
                        break;
                    }
                }
                if (flag){
                    ElderlyClass elderlyClass=new ElderlyClass();
                    elderlyClass.setElderlyClassCode(UUID.randomUUID().toString());
                    elderlyClass.setName("默认");
                    elderlyClass.setValue(100);
                    elderlyClass.setSort(100);
                    elderlyClass.setAddBalance(0);
                    elderlyClass.setStaffCode("a91b34cc-7e86-46e3-bf26-80e1a0bd0ab4");
                    elderlyClass.setGovernmentCode(government.getGovernmentCode());
                    elderlyClassService.insert(elderlyClass);
                }
            }

            return "ok!";
    }



}
